package com.hou.controller;


import com.hou.parma.PageParam;
import com.hou.pojo.*;
import com.hou.service.IBasicOrderService;
import com.hou.service.IOrderService;
import com.hou.service.ISeckillOrderService;
import com.hou.vo.CaseVip;
import com.hzk.vo.RespBean;
import com.hzk.vo.RespBeanEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @auoter:jackh
 * 订单的接口
 *
 */
@RestController
@Controller
@RequestMapping("/hou")
public class OrderController {
    @Autowired
    IBasicOrderService iOrderService;
    @Autowired
    ISeckillOrderService iSeckillOrderService;

/**
     *
     * @param pageParam
     * @return
     */

    @RequestMapping("/order/select")
    public RespBean order(@CaseVip Vip vip, PageParam pageParam){
        if (vip==null){
            return RespBean.error(RespBeanEnum.NULL_USER_ERROR);
        }
        Long aLong = iOrderService.selectCount();
        List list = iOrderService.selectList(pageParam);
        if (list==null){
            return RespBean.error(RespBeanEnum.NULL_ORDERS_ERROR);
        }
        return new RespBean(200,"SUCCESS",aLong,list);
    }
/**
     * 这个方法是按时间从古至今查询
     * @param pageParam
     * @return
     */


    @RequestMapping("/order/selectAsc")
    public RespBean app1(@CaseVip Vip vip,PageParam pageParam){
        if (vip==null){
            return RespBean.error(RespBeanEnum.NULL_USER_ERROR);
        }
        Long aLong = iOrderService.selectCount();
        List<BasicOrder> forms = iOrderService.selectList_Asc(pageParam);
        if (forms==null){
            return RespBean.error(RespBeanEnum.NULL_FORM_ERROR);
        }
        return new RespBean(200,"SUCCESS",aLong,forms);
    }

/**
     * 这一个方法是倒叙查询：从今至古
     * @param pageParam
     * @return
     */

    @RequestMapping("/order/selectDesc")
    public RespBean app(@CaseVip Vip vip,PageParam pageParam){
        if (vip==null){
            return RespBean.error(RespBeanEnum.NULL_USER_ERROR);
        }
        Long aLong = iOrderService.selectCount();
        List<BasicOrder> forms = iOrderService.selectList_Desc(pageParam);
        if (forms==null){
            return RespBean.error(RespBeanEnum.NULL_FORM_ERROR);
        }
        return new RespBean(200,"SUCCESS",aLong,forms);
    }

/**
     * 根据名称模糊查询
     * @param pageParam
     * @param
     * @return
     */


    @RequestMapping("/order/selectByid")
    public RespBean app4(@CaseVip Vip vip,PageParam pageParam){
        if (vip==null){
            return RespBean.error(RespBeanEnum.NULL_USER_ERROR);
        }
        Long aLong = iOrderService.selectCoun(Long.parseLong(pageParam.getSearch()));
        List<BasicOrder> forms = iOrderService.selectList_ByName(pageParam);
        if (forms==null){
            return RespBean.error(RespBeanEnum.NULL_FORM_ERROR);
        }
        return new RespBean(200,"SUCESSS",aLong,forms);
    }

/**
     * 查询通过的用户
     * @param pageParam
     * @return
     */
/*
    @RequestMapping("/app/selectByPass")
    public RespBean app2(PageParam pageParam){
        List<SeckillApplicationForm> forms = iSeckillApplicationFormService.selectList_ByPass(pageParam);
        if (forms==null){
            return RespBean.error(RespBeanEnum.NULL_FORM_ERROR);
        }
        return RespBean.success(forms);
    }*/
/**
     * 查询未通过的用户
     * @param
     * @return
     */


/*
@RequestMapping("/app/selectByFaile")
    public RespBean app3(PageParam pageParam){
        List<SeckillApplicationForm> forms = iSeckillApplicationFormService.selectList_ByFaile(pageParam);
        if (forms==null){
            return RespBean.error(RespBeanEnum.NULL_FORM_ERROR);
        }
        return RespBean.success(forms);
    }*/
    @RequestMapping("/order/add")
    public RespBean add(@CaseVip Vip vip,BasicOrder BasicOrder) {

        if (vip==null){
            return RespBean.error(RespBeanEnum.NULL_USER_ERROR);
        }
        //加这个查找id是否被注册，不然id值被注册了，直接报错400
        Long id = BasicOrder.getId();
        com.hou.pojo.BasicOrder byId = iOrderService.getById(id);
        if (byId != null){
            return RespBean.error(RespBeanEnum.OPRATION_ERROR);
        }

        boolean save = iOrderService.save(BasicOrder);
        SeckillOrder seckillOrder = new SeckillOrder();
        seckillOrder.setOrderId(BasicOrder.getId());
        seckillOrder.setGoodsId(BasicOrder.getGoodsId());
        seckillOrder.setUserId(BasicOrder.getUserId());
        seckillOrder.setId(BasicOrder.getId());
        iSeckillOrderService.save(seckillOrder);
        if (!save) {
            return RespBean.error(RespBeanEnum.OPRATION_ERROR);
        }
        return RespBean.success();
    }

    @RequestMapping("/order/update")
    public RespBean update(@CaseVip Vip vip,BasicOrder BasicOrder) {
        if (vip==null){
            return RespBean.error(RespBeanEnum.NULL_USER_ERROR);
        }
        boolean b = iOrderService.updateById(BasicOrder);
        SeckillOrder seckillOrder = new SeckillOrder();
        seckillOrder.setOrderId(BasicOrder.getId());
        seckillOrder.setGoodsId(BasicOrder.getGoodsId());
        seckillOrder.setUserId(BasicOrder.getUserId());
        seckillOrder.setId(BasicOrder.getId());
        boolean b1 = iSeckillOrderService.updateById(seckillOrder);
        if (!b) {
            return RespBean.error(RespBeanEnum.OPRATION_ERROR);
        }
        return RespBean.success();
    }

    @RequestMapping("/order/delete")
    public RespBean delete(@CaseVip Vip vip,Long id) {
        if (vip==null){
            return RespBean.error(RespBeanEnum.NULL_USER_ERROR);
        }
        boolean b = iOrderService.removeById(id);
        boolean b1 = iSeckillOrderService.removeById(id);
        if (!b) {
            return RespBean.error(RespBeanEnum.OPRATION_ERROR);
        }
        return RespBean.success();
    }
    @RequestMapping("/order/show")
    public RespBean app5(@CaseVip Vip vip,Long id){
        if (vip==null){
            return RespBean.error(RespBeanEnum.NULL_USER_ERROR);
        }

        BasicOrder order = iOrderService.selectList_Byid(id);
        if (order==null){
            return RespBean.error(RespBeanEnum.OPRATION_ERROR);
        }
        return RespBean.success(order);
    }
}
